package learn.java.basic.util.zip;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class CompressUseInflater {
    public static void main(String[] args) {
        String inputStr="hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "hello world.fdsaadfsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
                "";
        try {
            System.out.println(inputStr.length());
            byte[] outputStr = inputStr.getBytes("UTF-8");

            byte[] output =new byte[outputStr.length];
            Deflater compress = new Deflater(Deflater.BEST_COMPRESSION,false);
            compress.setInput(outputStr);
            compress.finish();
            int compressDataLength = compress.deflate(output);
            System.out.println(compressDataLength);

            //decompress
            Inflater decompress = new Inflater(false);
            byte[] input = new byte[outputStr.length];
            decompress.setInput(output, 0, compressDataLength);
            decompress.finished();
            int resultLength = decompress.inflate(input);
            System.out.println(resultLength);
            decompress.end();

            if (resultLength < compressDataLength) {
                System.out.println("负压缩比");
            }
            else {
                System.out.println(percnet( (double)compressDataLength , (double)resultLength));
            }
            System.out.println(new String(input, 0, resultLength,"UTF-8"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String percnet(double d,double e){
        double p = d/e;
        DecimalFormat nf = (DecimalFormat) NumberFormat.getPercentInstance();
        nf.applyPattern("00%"); //00表示小数点2位
        nf.setMaximumFractionDigits(2); //2表示精确到小数点后2位
        return nf.format(p);
    }
}
